package team.seekers.hula.question.questionUser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.question.question.service.QuestionService;
import team.seekers.hula.question.questionUser.entity.dao.QuestionUserDO;
import team.seekers.hula.question.questionUser.mapper.QuestionUserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户关注一个问题 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class QuestionUserService extends ServiceImpl<QuestionUserMapper, QuestionUserDO> {

    private boolean result;

    @Autowired
    @Lazy
    private QuestionService questionService;

    public void changeFollow(String questionId) {
        if(hasFollow(questionId,MindUtils.getUserId())){
            // 删除关注
            QueryWrapper<QuestionUserDO> wrapper = new QueryWrapper<>();
            wrapper.eq("question_id", questionId);
            remove(wrapper);

        }else {
            // 新增关注
            QuestionUserDO questionUserDO = new QuestionUserDO();
            questionUserDO.setQuestionId(questionId);
            questionUserDO.setUserId(MindUtils.getUserId());
            save(questionUserDO);

        }
    }

    //判断是否已经点过赞了，true为已经点过了
    public boolean hasFollow(String questionId,String userId){
        QueryWrapper<QuestionUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("question_id", questionId);
        wrapper.eq("user_id", userId);
        QuestionUserDO questionUserDO = getOne(wrapper);
        if(questionUserDO == null){
            return false;
        }else {
            return true;
        }
    }

    public Integer countFollower(String id) {
        return lambdaQuery().eq(QuestionUserDO::getQuestionId,id).count();
    }

    public Integer getFollowerCountByQuestionId(String questionId) {
        return lambdaQuery().eq(QuestionUserDO::getQuestionId,questionId).count();
    }
}
